import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/parts/api/v1';

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询出运确认书'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询出运确认书详情'
});
const newDataDesc = formatMessage({
    id: 'api.message.deliveryDesc',
    defaultMessage: '新增'
});
const updateDataDesc = formatMessage({
    id: 'api.message.deliveryOrderDesc',
    defaultMessage: '编辑'
});
const shipmentConfirmDesc = formatMessage({
    id: 'api.message.shipmentConfirmDesc',
    defaultMessage: '箱单发票'
});
const lcCreditsQueryDesc = formatMessage({
    id: 'api.message.lcCreditsQueryDesc',
    defaultMessage: '信用证'
});
const abandonDataDesc = formatMessage({
    id: 'api.message.abandonDataDesc',
    defaultMessage: '作废'
});

//初始化
export const getInitData = () => request(`${baseUrl}/ui/shipmentConfirm/init`, null, dto.InitDataStruct)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getListDesc));
// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/shipmentConfirms?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/shipmentConfirms/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
//新增
export const newData = data => request(`${baseUrl}/shipmentConfirms`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newDataDesc));
//修改
export const updateData = (id, data) => request(`${baseUrl}/shipmentConfirms/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDataDesc));
//箱单发票查询
export const partInvoicesQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partInvoices/forShipmentConfirm?${queryString}`, null, dto.ShipmentConfirmList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, shipmentConfirmDesc));
};
//箱单发票查询
export const lcCreditsQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/lcCreditManagements/forPartInvoice?${queryString}`, null, dto.ShipmentConfirmList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, lcCreditsQueryDesc));
};
//修改
export const abandonData = (id, data) => request(`${baseUrl}/shipmentConfirms/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDataDesc));
